<?php

namespace pam\Controller;

/**
 * Description of ResearchController
 *
 * @author Robert Bwana
 */
use Jaspersoft\Client\Client as JasperClient;
use pam\Service\researchServiceInterface as PSI;
use pam\Service\researchService;
use Zend\Mvc\Controller\AbstractActionController as AAC;
use Zend\View\Model\ViewModel;
use pam\Form\ResearchForm;
use pam\Model\Research;
use Zend\Form\FormInterface;

class ResearchController extends AAC {

    protected $researchService;
    protected $researchForm;

    public function __construct(researchService $researchService, FormInterface $form) {
        $this->researchService = $researchService;
        $this->researchForm = $form;
    }

    public function viewAction() {

        return new ViewModel(array(
            'research' => $this->researchService->findAll(),
        ));
    }

    public function printAction() {
        $this->exportReport();

        return $this->redirect()->toRoute('research');
    }

    public static function exportReport() {
        $client = new JasperClient(
                "http://reports.ssra.go.tz:8080/jasperserver", "jasperadmin", "jasperadmin"
        );
        $report = $client->reportService()->runReport('/reports/Research/SocialResearchReport');

        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Description: File Transfer');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . strlen($report));
        header('Content-Disposition: attachment; filename=research-research-report.pdf');
        header('Content-Type: application/pdf');

        echo $report;
        exit;
//return new ViewModel();
    }

    public function addAction() {
        $request = $this->getRequest();

        if ($request->isPost()) {
            $this->researchForm->setData($request->getPost());

            if ($this->researchForm->isValid()) {
                try {

                    $this->researchService->saveResearch($this->researchForm->getData());

                    return $this->redirect()->toRoute('research');
                } catch (\Exception $e) {
                    die($e->getMessage());
                }
            }
        }



        return new ViewModel(array(
            'form' => $this->researchForm
        ));
    }

    public function editAction() {
        $request = $this->getRequest();
        try {
            $research = $this->researchService->findResearch($this->params('id'));
        } catch (\InvalidArgumentException $e) {
            return $this->redirect()->toRoute('research');
        }
        $this->researchForm->bind($research);

        if ($request->isPost()) {
            $this->researchForm->setData($request->getPost());

            if ($this->researchForm->isValid()) {
                try {
                    $this->researchService->saveResearch($research);

                    return $this->redirect()->toRoute('research');
                } catch (\Exception $e) {
                    die($e->getMessage());
                }
            }
        }

        return new ViewModel(array(
            'form' => $this->researchForm
        ));
    }

    public function deleteAction() {
        try {
            $research = $this->researchService->findResearch($this->params('id'));
        } catch (\InvalidArgumentException $e) {
            return $this->redirect()->toRoute('research');
        }

        $request = $this->getRequest();

        if ($request->isPost()) {
            $del = $request->getPost('delete_confirmation', 'No');

            if ($del === 'Yes') {
                $this->researchService->deleteResearch($research);
            }

            return $this->redirect()->toRoute('research');
        }

        return new ViewModel(array(
            'research' => $research
        ));
    }

}

